package com.nulldev.util.graphics.misc;

import javax.swing.*;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.JVM;
import com.nulldev.util.graphics.renderIt.util.renderItUtils;
import com.nulldev.util.gui.fontAPI.FontManager;
import com.nulldev.util.gui.swingAPI.Swing;

import java.awt.*;
import java.awt.image.BufferStrategy;

public class GraphicsAccelDebug extends JFrame {

	private static final long serialVersionUID = -6305940081129549027L;

	public static void main(String[] args) {
		Arguments.hasArgument(args);
		Swing.enableJSO();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new GraphicsAccelDebug();
			}
		});
	}

	final GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
	BufferCapabilities bufferCapabilities;
	BufferStrategy bufferStrategy;

	int y = 0;
	int delta = 1;

	public GraphicsAccelDebug() {
		setTitle("Hardware Acceleration Test");
		setSize(500, 300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		setVisible(true);

		createBufferStrategy(2);
		bufferStrategy = getBufferStrategy();

		// sun.java2d.opengl.GLXGraphicsConfig
		bufferCapabilities = gc.getBufferCapabilities();

		JVM.println(renderItUtils.getDeviceConfigurationString(gc));
		JVM.println("name: " + bufferCapabilities.getClass().getName());

		new AnimationThread().start();
	}

	class AnimationThread extends Thread {
		@Override
		public void run() {

			while (true) {
				Graphics2D g2 = null;
				try {
					g2 = (Graphics2D) bufferStrategy.getDrawGraphics();
					draw(g2);
				} finally {
					if (g2 != null)
						g2.dispose();
				}
				bufferStrategy.show();

				try {
					// CHANGE HERE, DONT SLEEP
					// Thread.sleep(16);
				} catch (Exception err) {
					err.printStackTrace();
				}
			}
		}
	}

	private final Font font2d = FontManager.getFontSetAsAWT(16);

	public void draw(Graphics2D g2) {
		g2.setFont(font2d);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		if (!bufferCapabilities.isPageFlipping() || bufferCapabilities.isFullScreenRequired()) {
			g2.setColor(Color.black);
			g2.fillRect(0, 0, getWidth(), getHeight());
			g2.setColor(Color.red);
			g2.drawString("Hardware Acceleration is not supported...", 100, 100);
			g2.setColor(Color.white);
			g2.drawString("Page Flipping: " + (bufferCapabilities.isPageFlipping() ? "Available" : "Not Supported"), 100, 130);
			g2.drawString("Full Screen Required: " + (bufferCapabilities.isFullScreenRequired() ? "Required" : "Not Required"), 100, 160);
			g2.drawString("Multiple Buffer Capable: " + (bufferCapabilities.isMultiBufferAvailable() ? "Yes" : "No"), 100, 190);
		} else {
			g2.setColor(Color.black);
			g2.fillRect(0, 0, getWidth(), getHeight());
			g2.setColor(Color.white);
			g2.drawString("Hardware Acceleration is Working...", 100, 100);
			g2.drawString("Page Flipping: " + (bufferCapabilities.isPageFlipping() ? "Available" : "Not Supported"), 100, 130);
			g2.drawString("Full Screen Required: " + (bufferCapabilities.isFullScreenRequired() ? "Required" : "Not Required"), 100, 160);
			g2.drawString("Multiple Buffer Capable: " + (bufferCapabilities.isMultiBufferAvailable() ? "Yes" : "No"), 100, 190);
		}

		y += delta;
		if ((y + 50) > getHeight() || y < 0) {
			delta *= -1;
		}

		g2.setColor(Color.blue);
		g2.fillRect(getWidth() - 50, y, 50, 50);
	}
}